import http from "../untils/http";
import { AxiosRequestConfig } from 'axios'
//自定义各自数据请求封装
export default function request(config: AxiosRequestConfig) {
    const { url = '', method = 'GET', data = {}, headers = {} } = config
    switch (method.toUpperCase()) {
        case 'GET':
            return http.get(url, { params: data })
        case 'POST':
            // 表单提交  application/x-www-form-url-encoded
            if (headers['Content-Type'] === 'application/x-www-form-url-encoded') {
                // 转参数 URLSearchParams / 第三方库qs
                const p = new URLSearchParams()
                for (const key in data) {
                    p.append(key, data[key])
                }
                return http.post(url, p, { headers })
            }
            // 文件提交  multipart/form-data
            if (headers['content-type'] === 'multipart/form-data') {
                const p = new FormData()
                for (const key in data) {
                    p.append(key, data[key])
                }
                return http.post(url, p, { headers })
            }
            // 默认 application/json
            return http.post(url, data)
        case 'PUT': // 修改数据 --- 所有的数据的更新
            return http.put(url, data)
        case 'DELETE': // 删除数据

            return http.delete(url, { data })
        case 'PATCH': // 更新局部资源
            return http.patch(url, data)
        default:
            return http(config)
    }
}